package com.park.web.controller.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.park.client.model.question.QuestionTypeModel;
import com.park.client.service.question.QuestionTypeService;
import com.park.common.util.Page;
import com.park.common.util.PageData;
import com.park.common.util.Result;
import com.park.web.contant.Global;
import com.park.web.controller.BaseController;

@Controller
@RequestMapping("/admin/questionType")
public class AdminQuestionTypeController extends BaseController{

	@Autowired
	private QuestionTypeService questionTypeService;
	
	/**
	 * 问答类型页面
	 * @return
	 */
	@RequestMapping("questionTypeList")
	public String questionTypeList(HttpServletRequest request){
			return Global.ADMIN_PATH+"question/questionTypeList";
	}
	
	@RequestMapping("listRows")
	@ResponseBody
	public PageData<QuestionTypeModel> listRows(Integer rows,@RequestParam(name="page",defaultValue="1")Integer pageNow,HttpServletRequest request){
		PageData<QuestionTypeModel>  pageData = new PageData<>();
		QuestionTypeModel questionType = new QuestionTypeModel();
		Integer total = questionTypeService.selectTotal(questionType);
		Page page = new Page(pageNow,total,rows);
		questionType.setStartPos(page.getStartPos());
		questionType.setPageSize(page.getPageSize());
		List<QuestionTypeModel> list = questionTypeService.selectPagination(questionType);
		pageData.setTotal(total);
		pageData.setRows(list);
		return pageData;
	}
	
	

	@RequestMapping("save")
	@ResponseBody
	public Result<Boolean> save(QuestionTypeModel questionType,HttpServletRequest request){
		Result<Boolean> result = new Result<>(true);
		boolean success = questionTypeService.insertSelective(questionType);
		if(success){
			result.setMessage("添加成功!");
		}else{
			result.setMessage("添加失败!");
		}
		result.setSuccess(success);
		return result;
	}
	
	@RequestMapping("deleteById")
	@ResponseBody
	public Result<Boolean> deleteById(@RequestParam("id") Long id,HttpServletRequest request){
		Result<Boolean> result = new Result<>(true);
		boolean success = questionTypeService.deleteByPrimaryKey(id);
		if(success){
			result.setMessage("删除成功!");
		}else{
			result.setMessage("删除失败!");
		}
		result.setSuccess(success);
		return result;
	}
	
	@RequestMapping("edit")
	@ResponseBody
	public Result<Boolean> edit(QuestionTypeModel questionType,HttpServletRequest request){
		Result<Boolean> result = new Result<>(true);
		boolean success = questionTypeService.updateByPrimaryKeySelective(questionType);
		if(success){
			result.setMessage("修改成功!");
		}else{
			result.setMessage("修改失败!");
		}
		result.setSuccess(success);
		return result;
	}
	
	
	
}
